﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;

namespace Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule
{
    public class Purchaser
    {
        private Employee _employee;

        public Purchaser(Employee employee)
        {
            this._employee = employee;
        }

        public BoolGenericResult<Supplier> ContractSupplier(string id, SystemIdentifier.SupplierNature nature, string name, SupplierType type, string contact,
            string telephone, string mobile, string fax, string email,
            Area area, string address, string addressEnglish, string deliveryAddress1,
            string deliveryAddress2, SystemIdentifier.PayType payType, decimal discountRate, int payDay,
            decimal creditLimit, decimal defaultTaxRate, string remark)
        {
            BoolGenericResult<Supplier> result = new BoolGenericResult<Supplier>();

            Supplier supplier = SystemModuleFactory.CreateSupplier(id, nature, name, type, contact, telephone, mobile, fax, email, area, address, addressEnglish, deliveryAddress1, deliveryAddress2, payType, discountRate,
                payDay, creditLimit, defaultTaxRate, remark, this._employee.UserAccount.Id, this._employee.UserAccount.Role, this._employee.Corporation);

            result.Object = supplier;

            return result;
        }


        public BoolGenericResult<SupplierType> AddSupplierType(string name, int edituser)
        {
            BoolGenericResult<SupplierType> result = new BoolGenericResult<SupplierType>();
            SupplierType supplierType = StockModuleFactory.CreateSupplierType(name, edituser);
            result.Object = supplierType;
            return result;
        }

        public BoolGenericResult<SupplierType> UpdateSupplierType(SupplierType oldSupplierType, string name, int edituser)
        {
            BoolGenericResult<SupplierType> result = new BoolGenericResult<SupplierType>();
            oldSupplierType.Name = name;
            oldSupplierType.EditUser = edituser;
            oldSupplierType.ModifyDate = DateTime.Now;
            result.Object = oldSupplierType;
            return result;
        }

        public BoolGenericResult<Supplier> UpdateSupplier(Supplier oldSupplier, SystemIdentifier.SupplierNature nature, string name, SupplierType type, string contact,
            string telephone, string mobile, string fax, string email, Area area, string address, string addressEnglish, string deliveryAddress1,
            string deliveryAddress2, SystemIdentifier.PayType payType, decimal discountRate, int payDay, decimal creditLimit, decimal defaultTaxRate, string remark)
        {
            BoolGenericResult<Supplier> result = new BoolGenericResult<Supplier>();

            oldSupplier.Nature = nature;
            oldSupplier.Address = address;
            oldSupplier.AddressEnglish = addressEnglish;
            oldSupplier.Area = area;
            oldSupplier.Contact = contact;
            oldSupplier.CreditLimit = creditLimit;
            oldSupplier.DeliveryAddress1 = deliveryAddress1;
            oldSupplier.DeliveryAddress2 = deliveryAddress2;
            oldSupplier.DiscountRate = discountRate;
            oldSupplier.EditUser = this._employee.UserAccount.Id;
            oldSupplier.Email = email;
            oldSupplier.Fax = fax;
            oldSupplier.Mobile = mobile;
            oldSupplier.ModifyDate = DateTime.Now;
            oldSupplier.Name = name;
            oldSupplier.PayDay = payDay;
            oldSupplier.PayType = payType;
            oldSupplier.Remark = remark;
            oldSupplier.Telephone = telephone;
            oldSupplier.Type = type;

            result.Object = oldSupplier;

            return result;
        }

        public BoolGenericResult<PurchaseOrder> OrderStock(string id, DateTime purchaseDate, DateTime deliveryDate, string currency, decimal currencyRate, string supplierInvoiceNumber, string deliveryAddress, string remark, Employee purchaseEmployee, Project project, Supplier supplier, WareHouse wareHouse, string contact, string contactTelephone, IList<PurchaseOrderDetail> purchaseOrderDetails)
        {
            BoolGenericResult<PurchaseOrder> result = new BoolGenericResult<PurchaseOrder>();

            PurchaseOrder purchaseOrder = PurchaseModuleFactory.CreatePurchaseOrder(id, purchaseDate, deliveryDate, currency, currencyRate, supplierInvoiceNumber, deliveryAddress, remark, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id,
                this._employee.Corporation, this._employee, project, supplier, SystemIdentifier.CheckedStatus.UnChecked, PurchaseIdentifier.StockInStatus.UnIn, null, wareHouse, contact, contactTelephone);

            foreach (PurchaseOrderDetail pod in purchaseOrderDetails)
            {
                pod.PurchaseAmount = pod.Number * pod.Stock.StandardPurchasePrice;
                pod.TaxAmount = pod.Number * pod.Stock.StandardPurchasePrice * (pod.TaxRate / 100);
                pod.WholeAmount = pod.PurchaseAmount + pod.TaxAmount;
                pod.UnitCost = pod.WholeAmount / pod.Number;
                BoolGenericResult<PurchaseOrderDetail> addPurchaseOrderDetailResult = this.OrderStockDetail(pod.Number,
                        pod.UnitCost, pod.DiscountRate, pod.DiscountAmount, pod.PurchaseAmount, pod.WholeAmount, pod.TaxRate, pod.TaxAmount,
                        pod.Description, pod.Stock, pod.WareHouse);
                addPurchaseOrderDetailResult.Object.PurchaseOrder = purchaseOrder;
                purchaseOrder.PurchaseOrderDetails.Add(addPurchaseOrderDetailResult.Object);
            }

            result.Object = purchaseOrder;

            return result;
        }

        public BoolGenericResult<PurchaseOrderDetail> OrderStockDetail(decimal number, decimal unitCost, decimal discountRate, decimal discountAmount, decimal purchaseAmount, decimal wholeAmount, decimal taxRate, decimal taxAmount, string description, Stock stock, WareHouse wareHouse)
        {
            BoolGenericResult<PurchaseOrderDetail> result = new BoolGenericResult<PurchaseOrderDetail>();

            PurchaseOrderDetail purchaseOrderDetail = PurchaseModuleFactory.CreatePurchaseOrderDetail(number, unitCost, discountRate, discountAmount, purchaseAmount, wholeAmount, taxRate, taxAmount, description, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id,
                stock, wareHouse);
            purchaseOrderDetail.UnStockInNumber = number;
            purchaseOrderDetail.UnStockInAmount = number * unitCost;

            result.Object = purchaseOrderDetail;

            return result;
        }

        public BoolGenericResult<PurchaseOrder> UpdatePurchaseOrder(PurchaseOrder oldPurchaseOrder, DateTime purchaseDate, DateTime deliveryDate, string currency, decimal currencyRate, string supplierInvoiceNumber, string deliveryAddress,
            string remark, Employee purchaseEmployee, Project project, Supplier supplier, IList<PurchaseOrderDetail> purchaseOrderDetails)
        {
            BoolGenericResult<PurchaseOrder> result = new BoolGenericResult<PurchaseOrder>();

            oldPurchaseOrder.PurchaseDate = purchaseDate;
            oldPurchaseOrder.DeliveryDate = deliveryDate;
            oldPurchaseOrder.DeliveryAddress = deliveryAddress;
            oldPurchaseOrder.Currency = currency;
            oldPurchaseOrder.CurrencyRate = currencyRate;
            oldPurchaseOrder.SupplierInvoiceNumber = supplierInvoiceNumber;
            oldPurchaseOrder.Remark = remark;
            oldPurchaseOrder.PurchaseEmployee = purchaseEmployee;
            oldPurchaseOrder.Project = project;
            oldPurchaseOrder.Supplier = supplier;

            IList<long> deleted = new List<long>();
            foreach (PurchaseOrderDetail newPurchaseOrderDetail in purchaseOrderDetails)
            {
                if (newPurchaseOrderDetail.Id == 0)
                {
                    BoolGenericResult<PurchaseOrderDetail> addPurchaseOrderDetailResult = this.OrderStockDetail(newPurchaseOrderDetail.Number,
                        newPurchaseOrderDetail.UnitCost, newPurchaseOrderDetail.DiscountRate, newPurchaseOrderDetail.DiscountAmount,
                        newPurchaseOrderDetail.PurchaseAmount, newPurchaseOrderDetail.WholeAmount, newPurchaseOrderDetail.TaxRate,
                        newPurchaseOrderDetail.TaxAmount, newPurchaseOrderDetail.Description, newPurchaseOrderDetail.Stock, newPurchaseOrderDetail.WareHouse);
                    oldPurchaseOrder.PurchaseOrderDetails.Add(addPurchaseOrderDetailResult.Object);
                    continue;
                }
                if (newPurchaseOrderDetail.EditUser == -1)
                {
                    deleted.Add(newPurchaseOrderDetail.Id);
                    continue;
                }
            }
            foreach (PurchaseOrderDetail oldPurchaseOrderDetail in oldPurchaseOrder.PurchaseOrderDetails)
            {
                foreach (PurchaseOrderDetail newPurchaseOrderDetail in purchaseOrderDetails)
                {
                    if (oldPurchaseOrderDetail.Id == newPurchaseOrderDetail.Id)
                    {
                        oldPurchaseOrderDetail.Description = newPurchaseOrderDetail.Description;
                        oldPurchaseOrderDetail.DiscountAmount = newPurchaseOrderDetail.DiscountAmount;
                        oldPurchaseOrderDetail.DiscountRate = newPurchaseOrderDetail.DiscountRate;
                        oldPurchaseOrderDetail.Number = newPurchaseOrderDetail.Number;
                        oldPurchaseOrderDetail.PurchaseAmount = newPurchaseOrderDetail.PurchaseAmount;
                        oldPurchaseOrderDetail.Stock = newPurchaseOrderDetail.Stock;
                        oldPurchaseOrderDetail.TaxAmount = newPurchaseOrderDetail.TaxAmount;
                        oldPurchaseOrderDetail.TaxRate = newPurchaseOrderDetail.TaxRate;
                        oldPurchaseOrderDetail.UnitCost = newPurchaseOrderDetail.UnitCost;
                        oldPurchaseOrderDetail.WareHouse = newPurchaseOrderDetail.WareHouse;
                        oldPurchaseOrderDetail.WholeAmount = newPurchaseOrderDetail.WholeAmount;
                    }
                }
            }

            foreach (long id in deleted)
            {
                PurchaseOrderDetail deletedPurchaseOrderDetail = oldPurchaseOrder.PurchaseOrderDetails.FirstOrDefault(d => d.Id == id);
                oldPurchaseOrder.PurchaseOrderDetails.Remove(deletedPurchaseOrderDetail);
            }

            result.Object = oldPurchaseOrder;

            return result;
        }

        public AddPurchaseReturn ReturnOrderStock(string id, IList<StockInOutDetail> allStockInOutDetails, StockInOut stockInOut, DateTime returnDate,
    string supplierInvoiceNumber, string currency, decimal currencyRate, decimal taxRate, string receivingNumber, string invoiceDescription, string remark, string deliveryAddress,
    Employee purchaseEmployee, Project project, Supplier supplier, WareHouse wareHouse, string contact, string contactTelephone, StockInOutType type, Purchase purchase)
        {
            AddPurchaseReturn result = new AddPurchaseReturn();

            //按先进先出方式，更新有效库存数量
            decimal leftSubtractNumber = 0;
            foreach (StockInOutDetail newStockInOutDetail in stockInOut.StockInOutDetails)
            {
                leftSubtractNumber = newStockInOutDetail.Number;
                foreach (StockInOutDetail sid in allStockInOutDetails.OrderBy(s => s.CreateDate))
                {
                    if (newStockInOutDetail.Stock.Id == sid.Stock.Id)
                    {
                        if (leftSubtractNumber > 0 && sid.LeftNumber < leftSubtractNumber)
                        {
                            leftSubtractNumber -= sid.LeftNumber;
                            sid.LeftNumber = 0;
                        }
                        else
                        {
                            sid.LeftNumber -= leftSubtractNumber;
                            leftSubtractNumber = 0;
                        }
                    }
                }
            }

            StockManager stockManager = new StockManager(this._employee);
            IList<StockInOutDetail> stockInOutDetails = new List<StockInOutDetail>();
            foreach (StockInOutDetail newStockInOutDetail in stockInOut.StockInOutDetails)
            {
                if (newStockInOutDetail.Stock.LeftNumber < newStockInOutDetail.Number)
                {
                    result.IsSuccess = false;
                    result.Messages.Add("退货数量不得大于库存数量");
                    return result;
                }

                //创建入库单明细，负数
                BoolGenericResult<StockInOutDetail> addStockInOutDetail = stockManager.AddStockInDetail(null, -newStockInOutDetail.Number, newStockInOutDetail.UnitCost, StockIdentifier.CostType.StandardPrice, newStockInOutDetail.Number * newStockInOutDetail.UnitCost, newStockInOutDetail.TaxRate, newStockInOutDetail.DiscountRate,
                     remark, newStockInOutDetail.Stock, newStockInOutDetail.WareHouse);
                stockInOutDetails.Add(addStockInOutDetail.Object);
            }

            //生成退货单
            PurchaseReturn purchaseReturn = PurchaseModuleFactory.CreatePurchaseReturn(id, returnDate, supplierInvoiceNumber, currency, currencyRate, taxRate, receivingNumber, invoiceDescription, remark, deliveryAddress, DateTime.Now,
                DateTime.Now, this._employee.UserAccount, this._employee.Corporation, this._employee, project, supplier, null, SystemIdentifier.CheckedStatus.UnChecked, wareHouse, contact, contactTelephone, purchase);

            //生成出库单
            BoolGenericResult<StockInOut> addStockInResult = stockManager.AddStockOut(string.Empty, returnDate, type, purchaseReturn.Id, null, string.Empty, currency, currencyRate, remark, contact, contactTelephone,
                 null, purchaseEmployee, project, supplier, null, wareHouse, stockInOutDetails);

            result.Object = purchaseReturn;
            result.Object.StockInOut = addStockInResult.Object;

            //生成应付单，负数
            decimal amount = stockInOut.StockInOutDetails.Sum(s => (s.Number * s.UnitCost));
            UnPay unPay = PurchaseModuleFactory.CreateUnPay(result.Object.Id, PurchaseIdentifier.UnPayType.PurchaseReturn, 0, -amount, -amount, SystemIdentifier.PayType.Cash, PurchaseIdentifier.PayCondition.None,
    null, null, currency, currencyRate, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id, this._employee.Corporation, supplier);

            result.UnPay = unPay;
            return result;
        }

        public AddPurchaseReturn UpdatePurchaeReturn(PurchaseReturn oldPurchaseReturn, UnPay oldUnPay, IList<StockInOutDetail> allStockInOutDetails, DateTime returnDate, string supplierInvoiceNumber, string currency, decimal currencyRate,
            string receivingNumber, string invoiceDescription, string remark, string deliveryAddress, Employee purchaseEmployee, Project project, Supplier supplier,
            WareHouse wareHouse, string contact, string contactTelephone, StockInOut stockInOut)
        {
            AddPurchaseReturn result = new AddPurchaseReturn();
            //重新赋值
            oldPurchaseReturn.Currency = currency;
            oldPurchaseReturn.DeliveryAddress = deliveryAddress;
            oldPurchaseReturn.EditUser = this._employee.UserAccount.Id;
            oldPurchaseReturn.InvoiceDescription = invoiceDescription;
            oldPurchaseReturn.ModifyDate = DateTime.Now;
            oldPurchaseReturn.Project = project;
            oldPurchaseReturn.PurchaseEmployee = this._employee;
            oldPurchaseReturn.ReceivingNumber = receivingNumber;
            oldPurchaseReturn.Remark = remark;
            oldPurchaseReturn.ReturnDate = returnDate;
            oldPurchaseReturn.Supplier = supplier;
            oldPurchaseReturn.SupplierInvoiceNumber = supplierInvoiceNumber;
            oldPurchaseReturn.WareHouse = wareHouse;
            oldPurchaseReturn.Contact = contact;
            oldPurchaseReturn.ContactTelephone = contactTelephone;

            StockManager stockManager = new StockManager(this._employee);

            foreach (StockInOutDetail siod in stockInOut.StockInOutDetails.Where(s => s.Id == 0 || s.EditUser == -1))
            {
                //新增
                if (siod.Id == 0)
                {
                    //创建入库单明细，负数
                    BoolGenericResult<StockInOutDetail> addStockInOutDetail = stockManager.AddStockInDetail(null, -siod.Number, siod.UnitCost, StockIdentifier.CostType.StandardPrice, siod.Number * siod.UnitCost, siod.TaxRate, siod.DiscountRate,
                         remark, siod.Stock, siod.WareHouse);
                    oldPurchaseReturn.StockInOut.StockInOutDetails.Add(addStockInOutDetail.Object);
                    siod.Stock.LeftNumber -= siod.Number;
                    //重新计算有效库存的剩余数量
                    foreach (StockInOutDetail sid in allStockInOutDetails.OrderBy(s => s.CreateDate))
                    {
                        if (siod.Stock.Id == sid.Stock.Id)
                        {
                            sid.LeftNumber -= siod.Number;
                            break;
                        }
                    }
                }
                //删除
                if (siod.EditUser == -1)
                {
                    StockInOutDetail removed = oldPurchaseReturn.StockInOut.StockInOutDetails.FirstOrDefault(s=>s.Id == siod.Id);
                    oldPurchaseReturn.StockInOut.StockInOutDetails.Remove(removed);

                    foreach (StockInOutDetail sid in allStockInOutDetails.OrderBy(s => s.CreateDate))
                    {
                        if (siod.Stock.Id == sid.Stock.Id)
                        {
                            sid.LeftNumber += siod.Number;
                            siod.Stock.LeftNumber += siod.Number;
                            break;
                        }
                    }
                }
            }

            foreach (StockInOutDetail newStockInOutDetail in stockInOut.StockInOutDetails.Where(p => p.Id > 0 && p.EditUser != -1))
            {
                foreach (StockInOutDetail oldStockInOutDetail in oldPurchaseReturn.StockInOut.StockInOutDetails)
                {
                    //更新
                    if (newStockInOutDetail.Id == oldStockInOutDetail.Id)
                    {
                        oldStockInOutDetail.Description = newStockInOutDetail.Description;
                        oldStockInOutDetail.DiscountRate = newStockInOutDetail.DiscountRate;
                        oldStockInOutDetail.Number = newStockInOutDetail.Number;
                        oldStockInOutDetail.Stock = newStockInOutDetail.Stock;
                        oldStockInOutDetail.TaxRate = newStockInOutDetail.TaxRate;
                        oldStockInOutDetail.UnitCost = newStockInOutDetail.UnitCost;
                        oldStockInOutDetail.WareHouse = newStockInOutDetail.WareHouse;
                        oldStockInOutDetail.Cost = newStockInOutDetail.Number * newStockInOutDetail.UnitCost;

                        foreach (StockInOutDetail sid in allStockInOutDetails.OrderBy(s => s.CreateDate))
                        {
                            if (oldStockInOutDetail.Stock.Id == sid.Stock.Id)
                            {
                                if (newStockInOutDetail.Number > oldStockInOutDetail.Number)
                                {
                                    sid.LeftNumber -= newStockInOutDetail.Number - oldStockInOutDetail.Number;
                                    newStockInOutDetail.Stock.LeftNumber -= newStockInOutDetail.Number - oldStockInOutDetail.Number;
                                }
                                else
                                {
                                    sid.LeftNumber += oldStockInOutDetail.Number - newStockInOutDetail.Number;
                                    newStockInOutDetail.Stock.LeftNumber += oldStockInOutDetail.Number - newStockInOutDetail.Number;
                                }
                                break;
                            }
                        }
                    }
                }

            }
            if (oldUnPay.IsNotNull())
            {
                decimal amount = stockInOut.StockInOutDetails.Where(s => s.Id == 0 || (s.EditUser != -1)).Sum(s => (s.Number * s.UnitCost));
                oldUnPay.Cost = amount;
                oldUnPay.LeftAmount = oldUnPay.Cost;
                oldUnPay.Currency = currency;
                oldUnPay.CurrencyRate = currencyRate;
                oldUnPay.Supplier = supplier;
            }

            return result;
        }

        public AddPurchaseResult Purchase(string id, DateTime purchaseDate, Project project, Supplier supplier, string receivingNumber, string currency, decimal currencyRate, decimal taxRate,
            string supplierInvoiceNumber, string deliveryAddress, string remark, string invoiceDescription, decimal discount, SystemIdentifier.PayType payType,
            PurchaseIdentifier.PayCondition payCondition, DateTime? payDeadline, WareHouse wareHouse, string contact, string contactTelephone, Employee purchaseEmployee, PurchaseOrder purchaseOrder, StockInOut stockInOut,
            StockInOutType type)
        {
            AddPurchaseResult result = new AddPurchaseResult();

            decimal cost = 0;

            if (purchaseOrder.IsNotNull())
            {
                if (purchaseOrder.StockInStatus == PurchaseIdentifier.StockInStatus.Closed)
                {
                    result.IsSuccess = false;
                    result.Messages.Add("采购订单已关闭，不予入库");
                    return result;
                }
                if (purchaseOrder.StockInStatus == PurchaseIdentifier.StockInStatus.AllIn)
                {
                    result.IsSuccess = false;
                    result.Messages.Add("采购订单已全部入库，不再予入库");
                    return result;
                }
            }

            //生成采购单
            Purchase purchase = PurchaseModuleFactory.CreatePurchase(id, purchaseDate, project, supplier, receivingNumber, currency, currencyRate, taxRate, supplierInvoiceNumber,
                deliveryAddress, remark, invoiceDescription, discount, cost, payType, payCondition, payDeadline, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id,
                this._employee.Corporation, purchaseEmployee, purchaseOrder, wareHouse, contact, contactTelephone);


            IList<StockInOutDetail> stockInOutDetails = new List<StockInOutDetail>();
            foreach (StockInOutDetail siod in stockInOut.StockInOutDetails)
            {
                siod.Cost = siod.Number * siod.UnitCost;
                if (purchaseOrder.IsNotNull())
                {
                    foreach (PurchaseOrderDetail pod in purchaseOrder.PurchaseOrderDetails)
                    {
                        if (siod.Stock.Id == pod.Stock.Id)
                        {
                            if (pod.UnStockInNumber >= siod.Number)
                            {
                                //更新采购订单未入库数量
                                pod.UnStockInNumber = pod.UnStockInNumber - siod.Number;
                                pod.UnStockInAmount = pod.UnStockInAmount - (siod.Number * siod.UnitCost);
                                pod.StockInDate = DateTime.Now;
                            }
                            else
                            {
                                result.IsSuccess = false;
                                result.Messages.Add("入库数量不得大于采购数量");
                                return result;
                            }
                        }
                    }
                }


                //生成入库单明细
                StockInOutDetail stockInDetail = StockModuleFactory.CreateStockInDetail(null, siod.Number, siod.UnitCost, StockIdentifier.CostType.StandardPrice,
                    siod.Cost, siod.TaxRate, siod.DiscountRate, siod.Description, this._employee.UserAccount.Id, siod.Stock, siod.WareHouse);
                stockInOutDetails.Add(stockInDetail);
            }

            cost = stockInOut.StockInOutDetails.Sum(s => s.Cost);
            purchase.Cost = cost;

            result.Object = purchase;

            if (purchaseOrder.IsNotNull())
            {
                //更新采购订单入库状态
                purchaseOrder.StockInStatus = PurchaseIdentifier.StockInStatus.AllIn;
                foreach (PurchaseOrderDetail pod in purchaseOrder.PurchaseOrderDetails)
                {
                    if (pod.UnStockInNumber > 0)
                    {
                        purchaseOrder.StockInStatus = PurchaseIdentifier.StockInStatus.PartIn;
                        break;
                    }
                }
            }

            StockManager stockManager = new StockManager(this._employee);

            //生成入库单
            BoolGenericResult<StockInOut> addStockInResult = stockManager.AddStockIn(string.Empty, DateTime.Now, type, purchase.Id, null, string.Empty, currency, currencyRate,
                remark, contact, contactTelephone, null, purchaseEmployee, project, supplier, null, wareHouse, stockInOutDetails);
            result.Object.StockInOut = addStockInResult.Object;

            UnPay unPay = PurchaseModuleFactory.CreateUnPay(purchase.Id, PurchaseIdentifier.UnPayType.Purchase, 0, cost, cost, SystemIdentifier.PayType.Cash, PurchaseIdentifier.PayCondition.None,
                null, null, purchaseOrder.Currency, purchaseOrder.CurrencyRate, DateTime.Now, DateTime.Now, this._employee.UserAccount.Id, this._employee.Corporation, purchaseOrder.Supplier);
            result.UnPay = unPay;

            return result;
        }
    }
}
